package org.s3.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.s3.project.common.JwtUtils;
import org.s3.project.common.R;
import org.s3.project.entity.Dish;
import org.s3.project.entity.Merchant;
import org.s3.project.entity.User;
import org.s3.project.service.MerchantService;
import org.s3.project.util.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:29
 */
@CrossOrigin(origins = "*")
@RestController
public class MerchantController {
    @Autowired
   private   MerchantService merchantService;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    //商家登录
    @PostMapping("/loginMerchant")
    public Merchant login(String name, String password){
        return merchantService.login(name,password);
    }
    //查询商家
    @GetMapping("/selectMerchant")
    public List<Merchant> selectMerchant(){


        return merchantService.selectMerchant();
    }
    //根据id查询商家
    @GetMapping("/selectMerchantById/{id}")
    public Merchant selectMerchantById(@PathVariable("id") int id){
        return merchantService.selectMerchantById(id);
    }

    /**
     * 插入商户信息
     * 该方法通过POST请求接收一个Merchant对象，并将其保存到系统中
     * 主要用于处理商户信息的添加操作
     *
     * @param merchant 商户对象，包含商户的相关信息
     * @return 返回一个布尔值，表示商户信息是否成功保存
     */
    @PostMapping("/insertMerchant")
    public boolean insetMerchant(@RequestBody Merchant merchant) {
        return merchantService.save(merchant);
    }

    /**
     * 获取商家总数
     * @return 返回商家总数
     */
    @GetMapping("getMerchantCount")
    public Map<String, Object> getMerchantCount() {
        long goodsCount = merchantService.count();
        Map<String, Object> result = new HashMap<>();
        result.put("goodsCount", goodsCount);
        return result;
    }



    /**
     * 分页查询商家信息 状态为2的商家 2为审核中
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @return 商家信息列表
     */
    @GetMapping("showMerchantCheck/{page}")
    public R selectMerchantCheckPage(@PathVariable("page")int page, Integer pageSize,
                            @RequestParam(required = false)String name,
                            @RequestParam(required = false) String create_time,
                            @RequestParam(required = false) String create_timeOne){
        PageHelper.startPage(page,pageSize);//设置分页
        List<Merchant> merchantCheckList= this.merchantService.selectMerchantCheckLike(name,create_time,create_timeOne);
        PageInfo<Merchant> merchantCheckPageList = new PageInfo<>(merchantCheckList);//执行连表查询
        return new R().add("merchantCheckPageList",merchantCheckPageList);
    }

    /**
     * 更新商家状态
     * @param id 商家id
     * @param status 商家状态
     * @return 更新结果
     */
    @GetMapping("updateMerchantStatus")
    public int updateMerchantStatus(int id,int status){
        return merchantService.updateMerchantStatus(id,status);
    }

    /**
     * 批量通过或者不通过商家注册
     * @param ids 菜品id数组
     * @param status 状态
     * @return 修改结果
     */
    @GetMapping("updateMerchantStatusIds")
    public int updateMerchantStatusIds(int[] ids, int status) {
        return merchantService.updateMerchantStatusIds(ids,status);
    }

    //查询商家商品数量
    @GetMapping("selectMerchantProductCount/{merchant_id}")
    public int selectMerchantProductCount(@PathVariable("merchant_id")int merchant_id){
        return merchantService.selectMerchantProductCount(merchant_id);
    }
    //查询商家本月销售额
    @GetMapping("selectMerchantMonthSales/{merchant_id}")
    public int selectMerchantMonthSales(@PathVariable("merchant_id")int merchant_id){
        return merchantService.selectMerchantMonthSales(merchant_id);
    }

    //修改商家信息
    @PostMapping("/updateMerchant")
    public int updateMerchant(@RequestBody Merchant merchant){


        return merchantService.updateMerchant(merchant);
    }
    //上传图片
    @PostMapping("uploadImage")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("file = " + file.getOriginalFilename());
        return aliOSSUtils.upload(file);
    }

}
